let typeVal=1;
(function(){
      //日期选择函数
      function GetAnalysisWindowFiterTimes(valuetoid, type, event) {
            var ntime = new Date();
            if (type == 'd') {
                  var dstart = ntime.format("MM/dd/yyyy");
                  var dend = ntime.format("MM/dd/yyyy");
                  $(".timeControl #" + valuetoid).val(dstart + " - " + dend);
            } else if (type == 'w') {
                  var wstart = new Date(ntime.getTime() + (24 * 60 * 60 * -6 * 1000)).format("MM/dd/yyyy");
                  var wend = ntime.format("MM/dd/yyyy");
                  $(".timeControl #" + valuetoid).val(wstart + ' - ' + wend);
            } else if (type == 'm') {
                  var mstart = new Date(ntime.getTime() + (24 * 60 * 60 * -30 * 1000)).format("MM/dd/yyyy");
                  var mend = ntime.format("MM/dd/yyyy");
                  $(".timeControl #" + valuetoid).val(mstart + ' - ' + mend);
            }
            eval(event);
      }
      // GetAnalysisWindowFiterTimes('table_time', 'm', false); 
      $(".cycle span").on("click",function(){
            $(this).addClass("active").siblings().removeClass("active");
      })
      //选择日期触发事件
      $('.timeControl #table_time').daterangepicker(null, function (start, end, label) {
            times = $(".timeControl #table_time").val();
            showTypeVal(4)
            getTableData();
      }); 
      //获取table数据
      getTableData();
})();
function showTypeVal(val){
      typeVal=val;
}
function getTableData(){
      let times = $(".timeControl #table_time").val();
      var startTime="";
      var endTime="";
      if(times !== ""){
            var startTimeSplit = times.split('-')[0].replace(" ", "").split('/'),
            endTimeSplit = times.split('-')[1].replace(" ", "").split('/');
            startTime = startTimeSplit[2] + "-" + startTimeSplit[0] + "-" + startTimeSplit[1];
            endTime = endTimeSplit[2] + "-" + endTimeSplit[0] + "-" + endTimeSplit[1];  
      }
      
      var warningTypeVal="";
      let warningText=$(".dropdown-toggle")[0].innerHTML;
      if(warningText=="全部预警类型"){
            warningTypeVal="";
      }else if(warningText=="设备温度"){
            warningTypeVal=1;
      }else if(warningText=="设备超时未发送数据"){
            warningTypeVal=2;
      }else if(warningText=="数据超过阈值"){
            warningTypeVal=3;
      }
      layui.use(['table','layer','form','layedit','laydate'], function(){
            var table = layui.table;
            var layer=layui.layer;
            var layedit = layui.layedit;
            var form=layui.form;
            var $=layui.$;
            var laydate = layui.laydate;
            table.render({
                  elem: '#idTest',
                  url:'http://192.168.9.59:18902/alarm/findAll',
                  contentType: "application/x-www-form-urlencoded",
                  page: { 
                        layout: [ 'prev', 'page', 'next', 'skip','limit', 'count',] //自定义分页布局
                        , curr: 1 //设定初始在第 1 页
                        , limit: 10//每页多少数据
                        , groups: 5 //只显示 5 个连续页码
                        ,limits:[5,10,15]//每页条数的选择项 
                  },
                  where: {startTime:startTime,endTime:endTime,type:typeVal,warningType:warningTypeVal,id:null},
                  parseData: function(res){ //res 即为原始返回的数据
                              return {
                                    "code": res.code, //解析接口状态
                                    "msg": res.msg, //解析提示文本
                                    "count": res.total, //解析数据长度
                                    "data": res.data //解析数据列表
                              };
                        },
                  cols: [[
                              ,{field:'time',sort: true,title: '预警时间'}
                              ,{field:'deviceCode',sort: true, title: '设备编号'}
                              ,{field:'type2',sort: true, title: '设备预警类型'}
                              ,{field:'number',sort: true, title: '设备数值'}
                              ,{field:'state',sort: true, title: '预警级别'}
                              ,{field:'deviceName',sort: true, title: '设备名称'}
                              ,{field:'code',sort: true, title: '网点编号'}
                              ,{field:'name',sort: true, title: '网点名称'}
                              ,{field:'status',sort: true, title: '状态'}
                              ,{field:'dataCode',sort: true, title: '监测指标', width:250}
                              ,{field:'news',sort: true, title: '预警详细信息'}
                              ,{field:'progresType',sort: true, title: '传感器级别'}
                              ,{fixed: 'right', title:'操作', toolbar: '#barDemo',align:'center',templet: function (d) { return d.status}}
                              
                        ]],
                        done:function(res, curr, count){ //res 接口返回的信息
                              $("[data-field = 'type2']").children().each(function(){
                                    if($(this).text() == '1'){
                                          $(this).text("设备温度");
                                    }else if($(this).text() == '2'){
                                           $(this).text("设备超时未发送数据");
                                    }else if($(this).text() == '3'){
                                        $(this).text("数据超过阈值");
                                   }
                              })
                              $("[data-field = 'status']").children().each(function(){
                                    if($(this).text() == '0'){
                                          $(this).text("未处理");
                                    }else if($(this).text() == '1'){
                                           $(this).text("已处理");
                                    }
                              })
                        }   
                          
            });  
            table.on('tool(test)', function(obj){
                  var data = obj.data;
                  if(obj.event === 'see'){
                      //查看详情
                      let arr={};
                      $.post("http://192.168.9.59:18902/alarm/getDetail",{id:data.id}, function (res) {
                            if(res.msg=="查询成功"){
                              seeTable(res.data[0]);
                            }
                        //     layer.msg(res.msg)
                       });  
                  }
                  if(obj.event === 'handle'){
                        handle(data.id);
                  }
            })

      })
}
function seeTable(data){
      if(data != {}){
            layui.use(['table','layer','form','layedit','laydate'], function(){
                  var table = layui.table;
                  var layer=layui.layer;
                  var layedit = layui.layedit;
                  var form=layui.form;
                  var $=layui.$;
                  var laydate = layui.laydate;
      
                  layer.open({
                        type: 1,
                        title: ['查看','background-color: #3e4854;color:#fff'],
                        shadeClose: true,
                        shade: false,
                        skin:'layer-extskin-overauto',
                        maxmin: true, //开启最大化最小化按钮
                        area: ['980px', 'auto'],
                        content: $("#info"),
                        success:function(layero, index){
                              for(let i in data){
                                    if(data.type2 ==1){
                                          data.type2="设备温度";
                                    }else if(data.type2 ==2){
                                          data.type2="设备超时未发送数据";
                                    }else if(data.type2 ==3){
                                          data.type2="数据超过阈值";
                                    }
                                    if(data.status =='0'){
                                          data.status="未处理";
                                    }else if(data.status =='1'){
                                          data.status="已处理";
                                    }
                                    $('#'+ i).attr("disabled","disabled");
                                    $('#'+ i).val(data[i]);
                              }
                        },
                      
                  }); 
            
            })
      }
}
function handle(id){
      layui.use(['table','layer','form','layedit','laydate'], function(){
            var table = layui.table;
            var layer=layui.layer;
            var layedit = layui.layedit;
            var form=layui.form;
            var $=layui.$;
            layer.open({
                  type: 1,
                  title: ['预警处理','background-color: #3e4854;color:#fff'],
                  shadeClose: true,
                  shade: false,
                  skin:'layer-extskin-overauto',
                  maxmin: true, //开启最大化最小化按钮
                  area: ['400px', 'auto'],
                  content: $("#handledom"),
                  btn: ['保存','关闭'],
                  success:function(layero, index){
                        // $("#fromVal").addClass("layui-from");
                        layero.find(".layui-layer-btn0").attr("lay-filter","submitFilter").attr("lay-submit","")
                  },
                  yes: function(index, layero){
                        // 验证成功后执行操作
                        form.on('submit(submitFilter)',function () {
                              // var temp=$('#fromVal').val();
                              var temp=getElements("submitFrom")
                              if(temp){
                                    temp.SBJCYJ010=id;
                                    $.get("http://192.168.9.59:18902/alarm/submit",temp, function (res) {
                                          if(res.msg=="填报成功"){
                                                layer.closeAll(); 
                                                getTableData();
                                          }
                                          layer.msg(res.msg)
                                    }); 
                              }
                              return false;
                        })
                  },
                  btn2: function(){
                        layer.closeAll();
                  }
                
            }); 
      
      })
      // layer.confirm('确认处理该预警？', function () {
      //       $.get("http://192.168.9.59:18902/alarm/submit",{SBJCYJ010:data.id,SBUMITOR:111,SBUMITRESULT:1111111}, function (res) {
      //             if(res.msg=="填报成功"){
      //                   layer.msg(res.msg)
      //             }
      //        }); 
      // });
}
function getElements(formId) {
      debugger;
      var form = document.getElementById(formId);
      if(form == null){
            return false;
      }
      var elements = {};
      var inputTagElements = form.getElementsByTagName('input');
      for (var j = 0; j < inputTagElements.length; j++) {
            elements[inputTagElements[j].name] =inputTagElements[j].value;
      }
      return elements;
}

